{% extends "master.html" %}
{% load humanize %}
{% load custom %}

{% block subTitle %}{{map.title|escape}}{% endblock %}

{% block script %}
Go2.Init("{{username}}", "{{csrf}}");

var txtUsername;
var txtComment;

var tmUpdate;

function Loaded()
{
	txtUsername = document.getElementById("username")
	if (txtUsername)
		Go2.AddEventFn(txtUsername, "keydown", KeyDownUsername);

	txtComment = document.getElementById("comment");
	var objParams = Go2.ParseParams(window.location.href);
	if (objParams.comment)
		txtComment.value = objParams.comment;
	txtComment.focus();

	Go2.AddEventFn(txtComment, "keydown", KeyDownComment);
	
	tmUpdate = new Go2.Timer(Refresh, 60*1000).Active(true);
}

function Refresh()
{
	var sComment = "";
	if (txtComment.value != "")
		sComment = '?comment=' + encodeURIComponent(txtComment.value)
	window.location.href = "/info/{{map.GetId}}" + sComment;
}

function Click()
{
	// Call the Click function in our parent window in case we're in a Frame
	if (window.parent && window.parent.Go2MeParentClick != undefined)
		window.parent.Go2MeParentClick();
}

function PostComment()
{
	tmUpdate.Active(false);
	Go2.PostComment("{{map.GetId}}", txtUsername ? txtUsername.value : '', txtComment.value);
}

function KeyDownUsername(evt)
{
	if (evt.keyCode == 13)
		{
		txtComment.focus();
		evt.preventDefault();
		}
}	

function KeyDownComment(evt)
{
	if (evt.keyCode == 13)
		{
		PostComment();
		evt.preventDefault();
		}
}

window.onclick = Click;
{% endblock %}

{% block body %}

<div style="clear:both;"></div>

<div id="info-status">
	<p class="stats-header">
		<a style="float:right;padding-top:2px;padding-right:2px;" target="_top" href="{{map.Href}}" title="Close Frame"><img class="x" src="/images/x.png"/></a>
		Stats
	</p>
	<p>Created: {{map.dateCreated|Age}}</p>
	{% if map.Creator %}
	<p>By: <a href="/user/{{map.Creator}}" target="_top">{{map.Creator}}</a></p>
	{% endif %}
	<p>Shared: {{map.shareCount|intcomma}}</p>
	<p>Viewed: {{map.viewCount|intcomma}}</p>
	<p>Comments: {{map.CommentCount|intcomma}}</p>
	<p>Tags: 
	{% for tag in TopTags %}
		<a target="_top" href="/tag/{{tag}}" title="Recently popular | {{tag}}">{{tag|escape}}</a>{% if not forloop.last %}, {% endif %}
	{% endfor %}
	</p>
	{% if is_admin %}<p style="text-align:center;">
		<input type="button" onclick="Go2.BanishId('{{map.GetId}}', '{{map.Banished}}'=='False');" value="{% if map.Banished%}Un-{% endif %}Ban"/>
	</p>{% endif %}
</div> <!-- info-status -->

<div id="info-body">
<a target="_top" href="/" title="{{site_name}} Home | Popular Pages"><img class="logo-small" src="/images/logo-small.png"></a>
<h1><a target="_top" href="{{map.Href}}" title="{{map.url|escape}}">{{map.title|ellipsis:100|escape}}</a></h1>
<p class="url">{{map.url|escape}}</p>

<div id="info-form">
	{% if username %}
	<div style="float:left;width:70px;height:22px;margin-top:4px;">
	{% else %}
	<div style="float:left;width:220px;">
	{% endif %}
		{% if not username %}
		<label for="username">Nickname: </label>
		<input maxlength="20" style="width:70px;" type="text" id="username" name="username" tabindex="1"/>
		{% endif %}
		<label for="comment">Comment: </label>
	</div>

	<input onclick="PostComment();" style="float:right;width:70px;" type="button" value="Post" tabindex="3"/>

	{% if username %}
	<div style="margin-left:70px;margin-right:80px;">
	{% else %}
	<div style="margin-left:230px;margin-right:80px;">
	{% endif %}
		<input style="width:97%;" type="text" id="comment" name="comment" tabindex="2"/>
	</div>

	<p style="clear:left;" class="help">Comment format: "nickname: text of your comment [tag1, tag2, ...]"</p>
</div>

<div class="comments">
{% for comment in map.Comments %}
	{% spaceless %}<p>
    {% if comment.username %}<a target="_top" href="/user/{{comment.username}}" title="{{comment.username}}'s activity">{{comment.username}}</a>:{% endif %}
    {{comment.comment|escape|urlizetop}}
    {% if comment.tags %}
    	[{% for tag in comment.TagList %}<a target="_top" href="/tag/{{tag}}" title="{{tag}} pages">{{tag}}</a>{% if not forloop.last %}, {% endif %}{% endfor %}]
    {% endif %}
    - {{ comment.dateCreated|Age }}
    {% if comment.AllowDelete %}
    <img class="x" onclick="Go2.DeleteComment('{{comment.DelKey}}');" src="/images/x.png"/>
    {% endif %}
    </p>{% endspaceless %}
{% endfor %}
</div>

</div> <!-- info-body -->

{% endblock %}

